# -*- coding: utf-8 -*-

"""
DateTime   : 2020/12/17 15:49
Author     : ZhangYafei
Description: 保留原对象的指定属性信息
"""
import random
import time

from functools import wraps


def record_time(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        func(*args, **kwargs)
        end_time = time.time()
        print('spend is {}'.format(end_time - start_time))

    return wrapper


def func():
    print('func ...')


print('decorator before name is ', func.__name__)


@record_time
def func():
    print('func ...')


print('decorator after name is ', func.__name__)
"""
decorator before name is  func
decorator after name is  wrapper
"""

def record_time(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()
        func(*args, **kwargs)
        end_time = time.time()
        print('spend is {}'.format(end_time - start_time))

    return wrapper


def func():
    print('func ...')


print('decorator before name is ', func.__name__)


@record_time
def func():
    print('func ...')


print('decorator after name is ', func.__name__)

"""
decorator before name is  func
decorator after name is  func
"""


def record_time(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(f'{func.__name__}执行时间: {end - start:.3f}秒')
        return result

    return wrapper


@record_time
def download(filename):
    print(f'开始下载{filename}.')
    time.sleep(random.randint(2, 6))
    print(f'{filename}下载完成.')


@record_time
def upload(filename):
    print(f'开始上传{filename}.')
    time.sleep(random.randint(4, 8))
    print(f'{filename}上传完成.')


download('MySQL从删库到跑路.avi')
upload('Python从入门到住院.pdf')
# 取消装饰器
download.__wrapped__('MySQL必知必会.pdf')
upload = upload.__wrapped__
upload('Python从新手到大师.pdf')
